package com.wyw.function.learning.demo3;

import com.wyw.function.learning.common.Apple;
import com.wyw.function.learning.common.AppleClient;

import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

/**
 * @Title   Lambda 复合使用
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class DemoClient6 {

    static void printResult(List<Apple> apples) {
        AppleClient.prettyPrintApple(apples, apple -> "color:" + apple.getColor() + ", wight: " + apple.getWeight());
    }

    // 比较器复合
    public static void main(String[] args) {
        List<Apple> apples = AppleClient.getApples();
        // 倒叙
        apples.sort(Comparator.comparing(Apple::getWeight).reversed());
        printResult(apples);
    }

    // 比较器链：先根据weight倒序排，然后会根据color排
//    public static void main(String[] args) {
//        List<Apple> apples = AppleClient.getApples();
//        // 倒叙
//        apples.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
//    }

    // 谓词复合
    // 使用negate构造Predicate的非
//    public static void main(String[] args) {
//        List<Apple> apples = AppleClient.getApples();
//        Predicate<Apple> applePredicate = (a) -> a.getWeight() < 120;
//        List<Apple> collect = apples.stream().filter(applePredicate.negate()).collect(Collectors.toList());
//        collect.forEach(System.out::println);
//    }

    // 使用and进行组合
//    public static void main(String[] args) {
//        List<Apple> apples = AppleClient.getApples();
//        Predicate<Apple> applePredicate = (a) -> a.getWeight() > 120;
//        List<Apple> collect = apples.stream().filter(applePredicate.and((a) -> "red".equalsIgnoreCase(a.getColor()))).collect(Collectors.toList());
//        collect.forEach(System.out::println);
//    }

    // 使用or进行组合
//    public static void main(String[] args) {
//        List<Apple> apples = AppleClient.getApples();
//        Predicate<Apple> applePredicate = (a) -> a.getWeight() > 180;
//        List<Apple> collect = apples.stream().filter(applePredicate.or((a) -> "green".equalsIgnoreCase(a.getColor()))).collect(Collectors.toList());
//        collect.forEach(System.out::println);
//    }

    // 函数复合
//    public static void main(String[] args) {
//
//        Function<Integer, Integer> f = x -> x+1;
//        Function<Integer, Integer> g = x -> x*2;
//        Function<Integer, Integer> h = f.andThen(g); // g(f(x))    4
////        Function<Integer, Integer> h = f.compose(g);// f(g(x))    3
//
//        Integer apply = h.apply(1);
//        System.out.println(apply);
//    }

}
